package br.com.emefs1.ftgourmet.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.emefs1.ftgourmet.R;
import br.com.emefs1.ftgourmet.model.NavDrawerItem;

public class NavDrawerExpandableAdapter extends BaseExpandableListAdapter {

	private static int LAYOUT_GROUP_LIST_ITEM = R.layout.custom_list_view;
	private static int LAYOUT_GROUP_CHILD_LIST_ITEM = R.layout.custom_list_view_child;

	private HashMap<String, List<String>> childRecord;
	private LayoutInflater mInflater = null;
	private Activity context;

	private ArrayList<NavDrawerItem> navDrawerItems;

	public NavDrawerExpandableAdapter(Activity context,
			ArrayList<NavDrawerItem> navDrawerItems,
			HashMap<String, List<String>> childList) {

		this.navDrawerItems = navDrawerItems;
		this.childRecord = childList;
		this.context = context;
		this.mInflater = (LayoutInflater) this.context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public String getChild(int groupPosition, int childPosition) {
		return this.childRecord.get(
				((NavDrawerItem) getGroup(groupPosition)).getTitle()).get(
				childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		String childConfig = getChild(groupPosition, childPosition);

		ViewHolder holder;
		try {
			if (convertView == null) {
				holder = new ViewHolder();

				convertView = mInflater.inflate(LAYOUT_GROUP_CHILD_LIST_ITEM,
						null);
				holder.childTitle = (TextView) convertView
						.findViewById(R.id.childTitle);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.childTitle.setText(childConfig);

		} catch (Exception e) {
		}
		return convertView;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		NavDrawerItem parentSampleTo = navDrawerItems.get(groupPosition);

		ViewHolder holder;
		try {
			if (convertView == null) {
				convertView = mInflater.inflate(LAYOUT_GROUP_LIST_ITEM, null);
				holder = new ViewHolder();

				holder.parentTitle = (TextView) convertView
						.findViewById(R.id.parentTitle);
				holder.parentIcon = (ImageView) convertView
						.findViewById(R.id.parentIcon);
				holder.parentCount = (TextView) convertView
						.findViewById(R.id.parentCounter);

				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.parentTitle.setText(parentSampleTo.getTitle());
			holder.parentIcon.setBackgroundResource(parentSampleTo.getIcon());
			if (navDrawerItems.get(groupPosition).getCounterVisibility()) {
				holder.parentCount.setText(navDrawerItems.get(groupPosition)
						.getCount());
			} else {
				// hide the counter view
				holder.parentCount.setVisibility(View.GONE);
			}

		} catch (Exception e) {
		}
		return convertView;
	}

	public static class ViewHolder {

		private TextView childTitle;
		// Content
		private TextView parentTitle;
		private ImageView parentIcon;
		private TextView parentCount;

	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return this.childRecord.get(
				((NavDrawerItem) getGroup(groupPosition)).getTitle()).size();
	}

	@Override
	public NavDrawerItem getGroup(int groupPosition) {
		return this.navDrawerItems.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return this.navDrawerItems.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
